package cn.yh.springbootjwt.handler;

import cn.yh.springbootjwt.common.AjaxResult;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BindException.class)
    public AjaxResult handleException(BindException e) {
        return AjaxResult.error(e.getAllErrors().get(0).getDefaultMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public AjaxResult handleException(RuntimeException e) {
        return AjaxResult.error(e.getMessage());
    }
}
